import httpclient from 'httpclient';
import api from '../../../api/api_kind.js';
export default {
    name:"homePage",
    data: function () {
        return {
            editableTabsValue2: "",
            editableTabs2: [],
            userDialog: {
                visible: false,
            },
            passDialog: {
                data: {},
                visible: false,
                rules: {
                    oldPassword: [{required: true, message: '请输入登录密码', trigger: 'blur'}],
                    newPassword: [{required: true, message: '请输入新密码', trigger: 'blur'}],
                    checkPassword: [{required: true, message: '请输入确认新密码', trigger: 'blur'}],
                }
            },
            department: {
                visible: false,
            },
        };
    },
    watch: {
        '$route'(to) {
            let flag = false;//判断是否页面中是否已经存在该路由下的tab页
            //tabs记录当前页面中已存在的tab页
            for (let option of this.options) {
                //用名称匹配，如果存在即将对应的tab页设置为active显示桌面前端
                if (option.name === to.name) {
                    flag = true;
                    this.$store.commit('setActiveIndex', to.path);
                    break;
                }
            }
            //如果不存在，则新增tab页，再将新增的tab页设置为active显示在桌面前端
            if (!flag && to.path.split("/").length > 2) {
                this.$store.commit('addTabs', {route: to.path, name: to.name, meta: to.meta.title});
                this.$store.commit('setActiveIndex', to.path);
            }
            let offsetTop = document.getElementById("elTabs");
            if(null === offsetTop){
                let innerHeight = window.innerHeight  - 160;
                this.$store.commit("setContainerHeight","height:"+innerHeight+"px");
            }else{
                let elementAside = window.innerHeight - offsetTop.offsetTop - 60;
                this.$store.commit("setContainerHeight","height:"+elementAside+"px");
            }
        }
    },
    computed: {
        options() {
            return this.$store.state.tabStore["tabOptions"];
        },
    },
    methods: {
        onRandomStr() {
            return randomString(7).toString();
        },
        onUserDialog() {//我的信息
            this.userDialog.visible = true;
        },
        onPassDialog() {//修改密码
            this.passDialog.data={};
            this.passDialog.visible = true;
        },
        loginOut() {//注销登录
            this.$confirm('此操作将退出登录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                removeAllSessionStorage();
                removeAllLocalStorage();
                this.$store.commit("setMenu",[]);
                this.$router.push("/login");
            }).catch(() => {
                return false;
            });
        },
        //切换登录科室
        onDepartment() {
            this.department.visible = true;
        },
        //切换路由功能
        onRouteFuncs(data) {
            this.$store.commit("setRouteData", data);
            this.$store.commit('setTabOptions', []);
            this.$store.commit('setActiveIndex', '');
            this.$router.push({name: data.code, query: {_k: randomString(7)}});
        },
        //修改密码
        onEditPassword() {
            const that = this;
            this.$refs.passRef.validate((valid) => {
                if (valid) {
                    if(that.passDialog.data.checkPassword !== that.passDialog.data.newPassword){
                        httpclient.errorPage("error", "两次输入密码不一致", true);
                        return;
                    }
                    that.passDialog.data["username"] = that.$store.state.userStore.user.username;
                    httpclient.sendPut(api.LOGIN.EDIT_PASS, that.passDialog.data).done((res) => {
                        if (res.code === 1) {
                            removeAllSessionStorage();
                            removeAllLocalStorage();
                            httpclient.errorPage("success", res.errorMsg, true);
                            that.$router.push("/login");
                        } else {
                            httpclient.errorPage("error", res.errorMsg, true);
                        }
                    }).fail((error) => {
                        httpclient.errorPage("error", error.errorMsg, true);
                    });
                } else {
                    return false;
                }
            });
        }
    }
};
